const app = getApp();
Page({
  data: {
    order: null,
    first: true, //是否是第一次支付
    ticketType: 0,
    fileList: [],
    checked:false,
    acceptAdjust: '',
    ticket: '',
    ticketCode: '',
    imageMax: 0
  },
  previewImage(e) {
    const current = e.currentTarget.dataset.src;
    const urls = this.data.fileList.map(item => item.url);
    wx.previewImage({
      current: current,
      urls: urls
    });
  },
  onLoad(params) {
    const paramsObj = JSON.parse(decodeURIComponent(params.paramsStr))
    console.log(paramsObj);
    let ticket = '',ticketCode = '';
    let count = 0;
    let fileList = paramsObj.movieTickets && paramsObj.movieTickets.map(item=>{
      count = count + 1
      ticket = ticket + item.code[0].value
      ticketCode = ticketCode + item.code[1].value
      if (count != paramsObj.movieTickets.length) {
        ticket = ticket + ';'
        ticketCode = ticketCode + ';'
      }
      return {
        url: item.qrCode
      }
    })
    this.setData({
      ticket,
      ticketCode,
      fileList,
      imageMax: paramsObj.ticketNum,
      ticketList: ticket && ticket.split(';'),
      ticketCodeList: ticketCode && ticketCode.split(';'),
      acceptInfoList: paramsObj.acceptInfo && paramsObj.acceptInfo.split(';')
    })
    this.initData({
      ...paramsObj,
      differencePrice: ((paramsObj.ticketOriginalPrice - paramsObj.ticketUnitPrice).toFixed(2)) * paramsObj.ticketNum,
      totalPrice: paramsObj.ticketNum * paramsObj.ticketUnitPrice,
    })
  },
  initData(params) {
    this.setData({
      order: params
    })
  },
  copy(str) {
    const that = this;
    wx.setClipboardData({
      data: this.data.order.orderNo,
      success: function(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  afterRead(event) {
    const _this = this
    const { file } = event.detail;
    console.log(file);
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.globalData.url + '/appletSupplierRob/uploadPic', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'picFile',
      header:{
        consumerId: app.globalData.userInfo.consumerId
      },
      success(res) {
        // 上传完成需要更新 fileList
        console.log(JSON.parse(res.data).data);
        const { fileList } = _this.data;
        fileList.push({ ...file, qrCode: JSON.parse(res.data).data });
        _this.setData({ fileList });
      },
    });
  },
  onSwitchChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    if(!detail) {
      this.setData({
        acceptAdjust: ''
      })
    }
    this.setData({ checked: detail });
  },
  submit(){
    if(this.data.fileList.length == 0) {
      return wx.showToast({
        icon: 'none',
        title: '请输入上传图片',
      })
    }
    const movieTickets = this.data.fileList.map(item=> {
      return {
        code:[{
          text: this.data.ticket,
          value: this.data.ticketCode,
        }],
        qrCode: item.qrCode,
      }
    })
    const parms = {
      movieTickets,
      orderNo: this.data.order.orderNo,
      acceptAdjust: this.data.acceptAdjust
    }
    
    wx.request({
      url: app.globalData.url + '/appletSupplierRob/uploadTickets',
      method: 'POST',
      header:{
        consumerId: app.globalData.userInfo.consumerId,
        // "content-type": "application/x-www-form-urlencoded"
      },
      data: parms,
      success: function (res) {
        if(res.data.code == 200 ) {
          wx.showToast({
            icon: 'success',
            title: res.data.msg,
            duration: 2000,
            success:()=>{
              wx.navigateTo({
                url: "/pages/subPages/myOrder/myOrder",
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.msg,
          })
        }
      }
    })
  }
})